﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;
using CSP.Model.CSPBBS;
using CSP.Bll.IObjectBlls;
using CSP.Bll;
using System.Collections.ObjectModel;
using CSP.Web.UserControls;
using CSP.Web.Master;

namespace CSP.Web.MyHome.Authentication
{
    public partial class List : CSPMyHome
    {
        private int _personAlauthenticationType = 0;

        private int _enterpriseAlauthenticationType = 0;

        /// <summary>
        /// 个人认证类型0 未认证 1审核中 2认证失败 3认证成功
        /// </summary>
        protected int PersonAuthenticationType
        {
            get { return _personAlauthenticationType; }
        }

        /// <summary>
        /// 企业认证类型0 未认证 1审核中 2认证失败 3认证成功
        /// </summary>
        protected int EnterpriseAlauthenticationType
        {
            get { return _enterpriseAlauthenticationType; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            //指定被选中的导航
            ((HomeMaster)Master).SelectedNavigationLink = MyHomeLeftNavigationLink.Authentication;

            if (!IsPostBack)
            {
                if (Request.QueryString["op"] == "delete")
                {
                    //获取Post对象流 并转化为json字符串
                    //MemoryStream stream = .
                    //using (StreamReader stream = new StreamReader(Request.InputStream))
                    //{
                    //    jsonData = stream.ReadToEnd();
                    //}
                    Response.ClearContent();
                    Response.ContentType = "text/plain";
                    DeleteByIdAndUserName(Convert.ToInt32(Request.QueryString["id"]));
                    this.Response.End();
                }
                InitPage();
            }
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        private void InitPage()
        {
            LoadAuthenticationInfo();
            BindAuthentication();
        }


        /// <summary>
        /// 加载认证信息
        /// </summary>
        private void LoadAuthenticationInfo()
        {
            Member member = UserProvider.GetUserInfo(true);
            if (member != null && member.UserBasic != null)
            {
                _personAlauthenticationType = Convert.ToInt32(member.UserBasic.PersonalAuthentication);
                _enterpriseAlauthenticationType = Convert.ToInt32(member.UserBasic.EnterpriseAuthentication);
                //个人认证
                switch (_personAlauthenticationType)
                {
                    case 0://未作任何认证
                        {
                            this.LB_PersonalAuthenticationType.Text = "尚未认证";
                            break;
                        }
                    case 1://审核中
                        {
                            this.LB_PersonalAuthentication.Visible = false;
                            this.LB_PersonalAuthenticationType.Text = "认证审核中..";
                            break;
                        }
                    case 2://个人认证失败
                        {

                            this.LB_PersonalAuthenticationType.Text = "认证失败";
                            break;
                        }
                    case 3://个人认证和企业认证
                        {
                            this.LB_PersonalAuthentication.Visible = false;
                            this.LB_PersonalAuthenticationType.Text = "已经认证";
                            break;
                        }

                    default: { break; }
                }

                //企业认证
                switch (_enterpriseAlauthenticationType)
                {

                    case 0://未认证
                        {
                            this.LB_EnterpriseAuthenticationType.Text = "尚未认证";
                            break;
                        }
                    case 1://审核中
                        {
                            this.LB_EnterpriseAuthentication.Visible = false;
                            this.LB_EnterpriseAuthenticationType.Text = "认证审核中..";
                            break;
                        }
                    case 2://企业认证失败
                        {
                            this.LB_EnterpriseAuthenticationType.Text = "认证失败";
                            break;
                        }
                    case 3://企业认证
                        {
                            this.LB_EnterpriseAuthentication.Visible = false;
                            this.LB_EnterpriseAuthenticationType.Text = "已经认证";
                            break;
                        }
                    default: { break; }
                }
            }
        }

        /// <summary>
        /// 绑定证书
        /// </summary>
        private void BindAuthentication()
        {
            IEnumerable<CSP.Model.CSP.Authentication> certificates = GetCertificate();

            this.R_AuthenticationInfo.DataSource = certificates;
            this.R_AuthenticationInfo.DataBind();
        }

        /// <summary>
        /// 获取证书信息
        /// </summary>
        private IEnumerable<CSP.Model.CSP.Authentication> GetCertificate()
        {
            ICollection<CSP.Model.CSP.Authentication> result = null;
            CSPBllFactory _authenticationFactory = CSPBllFactory.GetInstance();
            using (IAuthenticationBll authenticationBll = _authenticationFactory.GetAuthenticationBll())
            {
                result = authenticationBll.GetByUser(UserProvider.UserName);
            }
            if (result == null)
            {
                result = new Collection<CSP.Model.CSP.Authentication>();
            }

            return result.Where(c => c.CertificateType != 1 && c.CertificateType != 0);

        }

        private void DeleteByIdAndUserName(int id)
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            CSP.Model.CSP.Authentication authentication = null;
            try
            {
                using (IAuthenticationBll authenticationBll = bllFactory.GetAuthenticationBll())
                {
                    authentication = authenticationBll.RemoveByIdAndUser(id, UserProvider.UserName);
                }
            }
            catch (Exception ex)
            {
                LoggerProvider.Error("删除失败,Id:" + id + ";用户名" + UserProvider.UserName, ex);
            }

            if (authentication == null)
            {
                this.Response.Write("{\"status\":0,\"msg\":\"操作失败!\"}");
            }
            else
            {
                this.Response.Write("{\"status\":1,\"msg\":\"操作成功!\"}");
            }
        }


    }
}